package com.itlong.cloud.filter;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import org.slf4j.MDC;

import java.util.Optional;

public class LogFilter extends Filter<ILoggingEvent> {

    @Override
    public FilterReply decide(ILoggingEvent event) {
        MDC.put("ERROR_TYPE", "");
        //如果是error级别 把第一个参数放入ERROR_TYPE中
        if (event.getLevel().levelInt == Level.ERROR_INT) {
            Object[] argumentArray = event.getArgumentArray();
            if (null != argumentArray && argumentArray.length != 0) {
                MDC.put("ERROR_TYPE", "【" + Optional.ofNullable(argumentArray[0]).orElse("") + "】");
            }
        }
        return FilterReply.ACCEPT;
    }
}

